# Maintainer: umarcor <unai.martinezcorral@ehu.eus>

_realname=ffts
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.0.0.r818.g2c8da48
pkgrel=1
pkgdesc="ffts: The Fastest Fourier Transform in the South  (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' )
url="https://github.com/ValveSoftware/ffts"
license=('custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc"
             git)
_commit='2c8da4877588e288ff4cd550f14bec2dc7bf668c'
source=("${_realname}::git+https://github.com/ValveSoftware/ffts.git#commit=${_commit}"
        "0001-CMake-Pretend-clang-is-gcc.patch"
        "0002-CMake-Install-dll-in-bindir.patch"
        "0003-CMake-Install-pkgconfig-file.patch"
        "0004-aarm64-valloc.patch")
sha256sums=('a10cb731e39cb9dcbd3b52d87188019d3a2de33d0fcd448ee7608c0b278e2bca'
            '8a93db6b71231d0483cc5a4f9e2ed615779034c837e7956850d4d1cb46b5ba01'
            '44bc988f91837096fc161223a7df8338e5574ace0e0c947a945370066e84f453'
            '4a67a37d9cf70f0b12f1b23c2344da7b94704213b8736d38e9bfe93bbd6aaffd'
            'afd4f65bf2f84be5c0ae0715f3942b8c3fa218d58297386303a3bde45c8780f6')

pkgver() {
  cd "${_realname}"
  echo '0.0.0.r'"$(git rev-list --count "${_commit}")"'.g'"$(git describe --all --long "${_commit}" | sed 's/^.*-g\(.*\)/\1/')"
}

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}"

  apply_patch_with_msg \
    0001-CMake-Pretend-clang-is-gcc.patch \
    0002-CMake-Install-dll-in-bindir.patch \
    0003-CMake-Install-pkgconfig-file.patch \
    0004-aarm64-valloc.patch
}

build() {
  cd "${srcdir}/${_realname}"
  mkdir build
  cd build
  MSYS2_ARG_CONV_EXCL=-DCMAKE_INSTALL_PREFIX= cmake \
    -GNinja \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DENABLE_SHARED=ON \
    ../
  cmake --build .
}

package() {
  cd "${srcdir}/${_realname}"/build

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}/bin/cmake.exe" --install ./

  install -Dm644 "${srcdir}/${_realname}/COPYRIGHT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT"
}
